<?php
/**
 * createImg.php
 * 股市 趋势图和线型图的function
 * @author:  Skiychan <developer@zzzzy.com>
 * @created: 2015/2/3
 * @modified:
 */

include "libs/pData.class.php";
include "libs/pDraw.class.php";
include "libs/pImage.class.php";

include "database.php";

/*
 * 创建图表
 * @param $stock_id int 股票id
 * @param $type line/bar 线形或柱形 默认线形
 */
function createImg($stock_id, $type = 'line')
{

    global $dbh;
    $tb_name = "tbl_stock_price_for_chart";
    $price_tbname = "tbl_stock_price";

    //条件 stock_id
    $wheres = " where stock_no = ".$stock_id;
    //排序 created
    $wheres .= " order by created asc";

    $sth = $dbh->prepare("SELECT * FROM ".$tb_name.$wheres);
    $sth->execute();
    $results = $sth->fetchAll(PDO::FETCH_ASSOC);

    foreach ($results as $keys => $values) :
        $line2[] = $values['current_price'];
        $bar[] = $values['volume'];
        
        //只显示整点的标签
        if ($keys % 4 == 0) {
            $times[] = $values['created'];
        } else {
            $times[] = VOID;
        }

    endforeach;

    $l2counts = count($line2);
    $myData = new pData();

    if ($type == "line") {

        //取出最值
        $sql = "SELECT  MIN(`current_price`) xiao, MAX(`current_price`) da FROM $tb_name $wheres";
        foreach ($dbh->query($sql, PDO::FETCH_ASSOC) as $row) {
            $bottom = (int)$row['xiao'] - 2;
            $top = (int)$row['da'] + 2;
        }

        //取出昨日收盘价
        $last_sql = "SELECT previous_close FROM `$price_tbname` where code = $stock_id";
        $price_sth = $dbh->prepare($last_sql);
        $price_sth->execute();
        $stock_last_price = $price_sth->fetch(PDO::FETCH_ASSOC);
        $previous_close = $stock_last_price['previous_close'];

        //昨日收盘价
        $l1s = array();
        for ($i = 1; $i <= $l2counts; $i++) {
            $l1s[] = $previous_close;
        }

        $myData->addPoints($l1s, "Line1");
        $myData->addPoints($line2, "Line2");

        $myData->setPalette("Line1", array("R" => 51, "G" => 114, "B" => 178));
        $myData->setPalette("Line2", array("R" => 0, "G" => 255, "B" => 0));

        $myData->setAxisPosition(0, AXIS_POSITION_RIGHT);
        $myData->addPoints($times, "Times");
        $myData->setSerieDescription("Times", "Time");
        $myData->setAbscissa("Times");
        $myData->setXAxisDisplay(AXIS_FORMAT_TIME, "H:i");

        $myPicture = new pImage(480, 300, $myData);

        //设置默认字体
        $myPicture->setFontProperties(array("FontName" => "fonts/en_us.ttf", "FontSize" => 6));

        //画格子和标签
        $myPicture->setGraphArea(10, 10, 450, 280);
        $AxisBoundaries = array(0 => array("Min" => $bottom, "Max" => $top));
        $Settings = array(
            "Mode" => SCALE_MODE_MANUAL,
            "GridR" => 200,
            "GridG" => 200,
            "GridB" => 200,
            "XMargin" => 0,
            "YMargin" => 0,
            //"DrawXLines" => false,
            "GridTicks" => 3,  //格子密度
            "ManualScale" => $AxisBoundaries,
        );
        $myPicture->drawScale($Settings);

        //画线
        $myPicture->drawLineChart();

        //设置Line1为无效,再画底色
        $myData->setSerieDrawable("Line1", FALSE);

        //画区域底线
        $area_arr = array(
            "ForceTransparency" => 15,  //透明度
        );
        $myPicture->drawAreaChart($area_arr);

    //柱形图
    } else {

        $myData->addPoints($bar, "Bar");
        $myData->setPalette("Bar", array("R" => 51, "G" => 114, "B" => 178));  //设置柱子的颜色
        $myData->addPoints($times, "Times");
        $myData->setSerieDescription("Times", "Time");
        $myData->setAbscissa("Times");
        $myData->setXAxisDisplay(AXIS_FORMAT_TIME, "H:i");

        $myPicture = new pImage(480, 200, $myData);

        //设置默认字体
        $myPicture->setFontProperties(array("FontName" => "fonts/en_us.ttf", "FontSize" => 6));
        $myPicture->Antialias = FALSE;

        $myPicture->setGraphArea(50, 20, 450, 180);

        //网格及坐标
        $scaleSettings = array(
            "Mode" => SCALE_MODE_START0,
            "GridR" => 200,
            "GridG" => 200,
            "GridB" => 200);
        $myPicture->drawScale($scaleSettings);

        $myPicture->drawBarChart();
    }
    $myPicture->stroke();
}